package com.compass.doctor.ui.mine

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.TextView
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import com.compass.doctor.R
import com.compass.doctor.databinding.ActivityPrescriptionRecordBinding
import com.compass.doctor.ui.patient.fragment.PrescribedFragment
import com.compass.framework.adapter.ViewPage2FragmentAdapter
import com.compass.doctor.base.BaseVBActivity
import com.compass.doctor.ui.mine.viewmodel.MineViewModel
import com.compass.framework.constant.KEY_ORDER_TYPE
import com.compass.framework.constant.REFRESH_RX
import com.compass.framework.constant.REFRESH_RX_CANCELLATION
import com.compass.framework.ext.dimenPx
import com.compass.framework.ext.gone
import com.compass.framework.ext.sp2px
import com.compass.framework.ext.visible
import com.compass.framework.model.PrescriptionStatus
import com.compass.framework.tablayout.DslTabLayout
import com.compass.framework.utils.BigDecimalUtils
import com.compass.framework.utils.LiveDataBus
import com.compass.framework.utils.dpToPx
import com.compass.framework.utils.getColorFromResource
import com.compass.framework.utils.getDrawableResource
import com.compass.framework.utils.spToPx

/**
 * Description： 处方记录
 * Date:2023/12/21 21:38
 */
class PrescriptionRecordActivity : BaseVBActivity<ActivityPrescriptionRecordBinding>() {

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, PrescriptionRecordActivity::class.java)
            context.startActivity(intent)
        }
    }

    private val mineViewModel: MineViewModel by viewModels()

    private lateinit var viewPageAdapter: ViewPage2FragmentAdapter
    private var fragments: ArrayList<Fragment> = ArrayList()
    private var statusList:MutableList<PrescriptionStatus> = mutableListOf()
    private var isFirst:Boolean=true
    override fun initView(savedInstanceState: Bundle?) {
        isFirst=true
        mineViewModel.rxStatusLiveData.observe(this) {
            it?.let {
                mBinding.tvTips.text = "处方总金额：${BigDecimalUtils.divide(it.prescriptionSum)}元"
                statusList=it.appDicDataVOList
                if (isFirst){
                    mBinding.dslTab.apply {
                        tabIndicator.indicatorWidth = -2
                        tabIndicator.indicatorEnableFlow = true
                        for (item in statusList) {
                            val rxFragment = PrescribedFragment()
                            val bundleRx = Bundle()
                            bundleRx.putString(KEY_ORDER_TYPE, item.value)
                            rxFragment.arguments = bundleRx
                            fragments.add(rxFragment)
                            addView(TextView(this@PrescriptionRecordActivity).apply {
                                text = item.label
                                textSize = 16f
                                gravity = Gravity.CENTER_HORIZONTAL
                                layoutParams = DslTabLayout.LayoutParams(-2, dpToPx(85)).apply {
                                    topMargin = dpToPx(15)
                                }
                            })
                        }
                        onTabBadgeConfig={_, tabBadge, index ->
                            tabBadge.badgeText=  statusList[index].count.toString()
                            null
                        }

                    }
                    initDslTab()
                }else{
                    for (i in statusList.indices){
                        mBinding.dslTab.updateTabBadge(i,statusList[i].count.toString())
                    }

                }



            }
        }
        mineViewModel.getPrescriptionRecordStatus()


    }

    override fun initData() {
        LiveDataBus.get().with(REFRESH_RX_CANCELLATION, String::class.java).observe(this) {
            isFirst=false
            mineViewModel.getPrescriptionRecordStatus()
        }
    }

    private fun initDslTab() {
        mBinding.dslTab.configTabLayoutConfig {
            onSelectIndexChange = { _, selectIndexList, _, _ ->
                val toIndex = selectIndexList.first()
                if (toIndex==0) mBinding.tvTips.visible() else mBinding.tvTips.gone()
                mBinding.viewpager2.setCurrentItem(toIndex, false)

            }
        }
        viewPageAdapter = ViewPage2FragmentAdapter(supportFragmentManager, lifecycle, fragments)
        mBinding.viewpager2.adapter = viewPageAdapter
        mBinding.viewpager2.isUserInputEnabled = false//禁止viewPage滑动
        mBinding.viewpager2.setCurrentItem(0, false)
    }



}